package com.ssyt.examinationsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ssyt.examinationsystem.entity.CourseInfo;
import com.ssyt.examinationsystem.entity.JudgeInfo;
import com.ssyt.examinationsystem.entity.PageBean;
import com.ssyt.examinationsystem.service.IJudgeInfoService;
import com.ssyt.examinationsystem.service.impl.JudgeInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Servlet implementation class JudgeInfoController
 */
public class JudgeInfoController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public JudgeInfoController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     * response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 创建业务对象
        IJudgeInfoService judgeService = new JudgeInfoServiceImpl();

        PrintWriter out = response.getWriter();
        // 获取地址栏上的flag值
        String flag = request.getParameter("flag");
        if ("query".equals(flag)) {
            query(request, response);
        } else if ("judge".equals(flag)) {
            judge(judgeService, request, response);
        } else if ("list".equals(flag)) {
            list(judgeService, request, response, out);
        } else if ("add".equals(flag)) {
            add(judgeService, request, response, out);
        } else if ("dele".equals(flag)) {
            delete(judgeService, request, response, out);
        } else if ("update".equals(flag)) {
            update(judgeService, request, response, out);
        }

        out.flush();
        out.close();
    }

    /**
     * 修改判断题
     *
     * @param judgeService
     * @param request
     * @param response
     * @param out
     */
    private void update(IJudgeInfoService judgeService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        // 获取表单数据
        String question = request.getParameter("question");
        String answer = request.getParameter("answer");
        String courseId = request.getParameter("courseId");
        String judgeId = request.getParameter("judgeId");
        // 创建一个判断题对象，并封装属性
        JudgeInfo judge = new JudgeInfo();
        judge.setQuestion(question);
        judge.setAnswer(answer);
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf("".equals(courseId) ? "0" : courseId));
        judge.setCourse(course);
        judge.setJudgeId(Integer.valueOf("".equals(judgeId) ? "0" : judgeId));
        // 调用添加的业务方法
        int row = judgeService.updateJudge(judge);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 删除判断题
     *
     * @param judgeService
     * @param request
     * @param response
     * @param out
     */
    private void delete(IJudgeInfoService judgeService, HttpServletRequest request, HttpServletResponse response,
                        PrintWriter out) {
        String judgeId = request.getParameter("judgeId");
        if (judgeService.deleteJudge(judgeId) > 0) {
            out.print("ok");
        }
    }

    /**
     * 添加判断题的方法
     *
     * @param judgeService
     * @param request
     * @param response
     * @param out
     */
    private void add(IJudgeInfoService judgeService, HttpServletRequest request, HttpServletResponse response,
                     PrintWriter out) {
        // 获取表单数据
        String question = request.getParameter("question");
        String answer = request.getParameter("answer");
        String courseId = request.getParameter("courseId");
        // 创建一个判断题对象，并封装属性
        JudgeInfo judge = new JudgeInfo();
        judge.setQuestion(question);
        judge.setAnswer(answer);
        CourseInfo course = new CourseInfo();
        course.setCourseId(Integer.valueOf("".equals(courseId) ? "0" : courseId));
        judge.setCourse(course);
        // 调用添加的业务方法
        int row = judgeService.addJudgeInfo(judge);
        if (row > 0) {
            out.print("ok");
        }
    }

    /**
     * 分页查询所有判断题
     *
     * @param judgeService
     * @param request
     * @param response
     * @param out
     */
    private void list(IJudgeInfoService judgeService, HttpServletRequest request, HttpServletResponse response,
                      PrintWriter out) {
        PageBean page = new PageBean();
        // 获取当前页
        String current = request.getParameter("page");
        // 获取每页显示数
        String pageSize = request.getParameter("rows");
        // 获取页面传入当前页
        if (current != null) {
            Integer currentPage = Integer.valueOf(current);
            page.setCurrentPage(currentPage);
            page.setPageSize(Integer.valueOf(pageSize));
        }

        PageBean pageBean = judgeService.getJudgeByPage(page);

        Map<String, Object> jsonData = new HashMap<String, Object>();
        jsonData.put("total", pageBean.getTotalRecords());
        jsonData.put("rows", pageBean.getList());
        String jsonDataString = JSON.toJSONString(jsonData, true);

        out.print(jsonDataString);

    }

    /**
     * 将判断题显示在选题页面
     *
     * @param judgeService
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void judge(IJudgeInfoService judgeService, HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Map<String, Object>> judges = judgeService.getJudge();
        request.setAttribute("judges", judges);
        // 转发到选题页面
        request.getRequestDispatcher("WEB-INF/views/group/selectQuestion.jsp").forward(request, response);

    }

    private void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 转发到填空题显示页面
        request.getRequestDispatcher("WEB-INF/views/question/judge.jsp").forward(request, response);

    }

}
